package com.candoit.webapps.pricing.controllers;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import com.candoit.webapps.pricing.model.CentroCostos;
import com.candoit.webapps.pricing.model.CentroCostosCriterio;
import com.candoit.webapps.pricing.model.Cuenta;
import com.candoit.webapps.pricing.model.CuentaMes;
import com.candoit.webapps.pricing.util.CentroUtil;
import com.candoit.webapps.pricing.util.QueryUtil;

@ManagedBean(name = "centerCostsCtrl")
@SessionScoped
public class CenterCostsController extends AbstractController {
	
	private List<CentroUtil> centros;
	
	private CentroCostos centroSelected;
	
	public List<CentroUtil> getCentros() {
		List<CentroCostos> centrosList = QueryUtil.getInstance().getCentroCostos();
		this.fillCentros(centrosList);
		return centros;
	}

	public void setCentros(List<CentroUtil> centros) {
		this.centros = centros;
	}

	public CentroCostos getCentroSelected() {
		if (centroSelected == null){
			centroSelected = new CentroCostos();
		}
		return centroSelected;
	}

	public void setCentroSelected(CentroCostos centroSelected) {
		this.centroSelected = centroSelected;
	}

	public void saveCentro(){
		Iterator<CentroUtil> it = centros.iterator();
		while (it.hasNext()){
			   CentroUtil util = (CentroUtil) it.next();
			   if (util.getCentro().getId() == null || util.getCentro().getId() == 0){
				   if (util.getCentro().getNombre() != null && !util.getCentro().getNombre().equals("")){
					   this.save(util.getCentro());
				   }
			   }else{
				   this.update(util.getCentro());
			   }
		}
	}
	
	public void removeCentro(){
		Iterator<CentroUtil> it = centros.iterator();
		while (it.hasNext()){
			CentroUtil util = (CentroUtil) it.next();
			if (util.isSelected() && util.getCentro().getId() != null && util.getCentro().getId() != 0){
				List<CentroCostosCriterio> centrosCriterio = QueryUtil.getInstance().getCentroCostoCriterio(util.getCentro());
				Iterator<CentroCostosCriterio> itCrit = centrosCriterio.iterator();
				while (itCrit.hasNext()){
						CentroCostosCriterio ccCrit = (CentroCostosCriterio) itCrit.next();
						this.remove(ccCrit);
				}
				
				List<Cuenta> cuentas = QueryUtil.getInstance().getCuentasByCentro(util.getCentro());
				Iterator<Cuenta> itCuenta = cuentas.iterator();
				while (itCuenta.hasNext()){
					  Cuenta cuenta = (Cuenta) itCuenta.next();
					  List<CuentaMes> cuentasMes = QueryUtil.getInstance().getCuentaMesByCuenta(cuenta);
					  for (CuentaMes cm : cuentasMes){
						   this.remove(cm);
					  }
					  this.remove(cuenta);
				}
				
				this.remove(util.getCentro());
			}	
		}	
	}
	
	public void clearData(){
		this.centroSelected = null;
		this.saveSuccess = false;
		this.deleteSuccess = false;
	}
	
	private void fillCentros(List<CentroCostos> list){
		centros = new ArrayList<CentroUtil>();
		Iterator<CentroCostos> it = list.iterator();
		while (it.hasNext()){
			   CentroCostos centro = (CentroCostos) it.next();
			   CentroUtil util = new CentroUtil();
			   util.setCentro(centro);
			   util.setSelected(false);
			   centros.add(util);
		}
		
		CentroUtil util = new CentroUtil();
		CentroCostos centro = new CentroCostos();
		util.setCentro(centro);
		util.setSelected(false);
		centros.add(util);
	}
	
	public void saveMaquinas(){
		this.update(centroSelected);
	}
}
